<?php

class Reports_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function get_report_requests($where = array(), $where_notin = array())
    {
		$this->db->select('*');
		$this->db->from('reports');
		$this->db->where($where);
        
        if(!empty($where_notin))
            $this->db->where_not_in('report_status', $where_notin);

		return $this->db->get()->result_array();
		
    }

    function check_report_request($where)
    {
		$query = "Select * from reports WHERE 1=1 ";
		
		if( $where['merchant_id'] )
			$query .= " AND merchant_id='{$where['merchant_id']}'";
		
		if( $where['report_type'] )
			$query .= " AND report_type='{$where['report_type']}'";
		
		if( $where['date_interval'] )
			$query .= " AND date_interval='{$where['date_interval']}'";
		
		if( $where['from_date'] )
			$query .= " AND DATE(from_date)='{$where['from_date']}'";
		
		if( $where['to_date'] )
			$query .= " AND DATE(to_date)='{$where['to_date']}'";
		
		
		return $this->db->query($query)->row_array();
		
    }
    
    function is_exists($where = array())
    {
    	$this->db->select('*');
		$this->db->from('reports');
		$this->db->where($where);
       
		return $this->db->get()->num_rows();
    }

    function get_older_reports( $age = 30 )
    {
    	$query = "SELECT * FROM reports where NOW()>DATE_ADD(created_time, INTERVAL $age DAY)";
    	
    	return $this->db->query($query)->result_array();
    }

}